<?php

namespace App\Services;

use App\Jobs\StatisticLogJob;
use App\Models\StatisticLog;
use App\Repositories\Contracts\NewsRepository;

class NewsService
{
    private $newsRepository;

    public function __construct(NewsRepository $newsRepository)
    {
        $this->newsRepository = $newsRepository;
    }

    /**
     * 获取列表
     *
     * @param array|null $param
     */
    public function getList($param = null)
    {
        try {
            $filter = [
                'type' => $param['type'] ?? 1,
                'keyword' => $param['keyword'] ?? '',
            ];

            $result = $this->newsRepository->paginate($param['per_page'] ?? null, $filter);
            return $result;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 获取详情
     *
     * @param int $id
     * @return object|array
     */
    public function getInfo($id)
    {
        try {
            $result = $this->newsRepository->find($id);

            if (!empty($result['data'])) {
                $type = 0;
                if ($result['data']['type'] == 1 || $result['data']['type'] == 2) {
                    switch ($result['data']['type']) {
                        case 1:
                            $type = StatisticLog::TYPE_CULTURE_INFO;
                            break;
                        case 2:
                            $type = StatisticLog::TYPE_NEWS_INFO;
                            break;
                        default:
                            $type = StatisticLog::TYPE_CULTURE_INFO;
                            break;
                    }
                }
                if ($type > 0) {
                    //更新点击量
                    $params = [
                        'type' => $type,
                        'res_id' => $id,
                        'user_id' => request()->user('api')->user_id ?? 0,
                        'user_ip' => get_client_ip(),
                    ];
                    StatisticLogJob::dispatchNow($params);
                }

            }
            return $result;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 新增
     *
     * @param array $param
     */
    public function add($param)
    {
        try {

            $news = $this->newsRepository->store($param);
            return $news;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 修改
     *
     * @param array $param
     * @param int $id
     */
    public function update($param, $id)
    {
        try {
            $news = $this->newsRepository->update($param, $id);
            return $news;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 删除
     *
     * @param int $id
     */
    public function delete($id)
    {
        try {
            $news = $this->newsRepository->delete($id);

            $result = [];
            if ($news) {
                $result = 'success';
            } else {
                $result = 'error';
            }
            return $result;

        } catch (\Throwable $th) {
            throw $th;
        }
    }
}
